<template>
  <div class="my-cover">
    <div class="operateBox" @click="openDialog">
      <span>选择素材</span>
      <img :src="imgurl" class="img">
    </div>
    <!-- 对话框 -->
    <el-dialog
      :append-to-body="true"
      :visible.sync="dialogVisible"
      width="720px">
      <el-card class="box-card">
        <div style="padding-bottom: 20px;">
          <el-radio-group v-model="collect" size="mini" @change="collectChange">
            <el-radio-button :label="false">全部</el-radio-button>
            <el-radio-button :label="true">收藏</el-radio-button>
          </el-radio-group>
        </div>
        <el-row :gutter="10">
          <el-col
            @click.native="selectHandle(image)"
            v-for="(image) in imageList"
            :key="image.id" class="img_item" :xs="12" :sm="6" :md="6" :lg="4">
            <div class="img_box">
              <img :src="image.url" alt="">
              <div class="selected" v-show="image.isFlag">
                <img src="./img/selected.png" alt="">
              </div>
            </div>
          </el-col>
        </el-row>
        <el-pagination
          style="margin-top:10px;"
          background
          layout="sizes, prev, pager, next"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          @current-change="hPageChange"
          :total="total_count">
        </el-pagination>
      </el-card>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getImages } from '@/api/image'
export default {
  name: 'Cover',
  props: {
    imgurl: {
      type: String
    }
  },
  data () {
    return {
      imageList: [], // 素材列表
      curPage: 1, // 页码
      total_count: 0, // 总条数
      per_page: 12, // 每页显示多少条数据
      collect: false, // true显示收藏的  false显示全部
      dialogVisible: false, // 控制弹框显示隐藏
      curImage: null // 缓存当前选中的图片素材
    }
  },
  methods: {
    openDialog () {
      this.dialogVisible = true
    },
    selectHandle (image) {
      this.imageList.forEach((item) => {
        item.isFlag = false
      })
      image.isFlag = true
      this.curImage = image
    },
    loadImages () {
      getImages({
        collect: this.collect, // true显示收藏的  false显示全部
        per_page: this.per_page, // 每页显示多少条数据
        page: this.curPage // 页码
      }).then(res => {
        // 添加自定义的参数
        // 修改一下源数据给每一项都添加一个字段专门用来控制操作区域是否显示
        const sourceData = res.data.data.results
        const newData = sourceData.map((item) => {
          return {
            ...item,
            isFlag: false // 这个字段用来控制操作区域是否显示
          }
        })
        this.imageList = newData // 从新赋值给列表
        this.total_count = res.data.data.total_count
      })
    },
    // 分页加载的逻辑
    hPageChange (curPage) {
      // 获取页码
      this.curPage = curPage
      this.loadImages()
    },
    // 方法调用时机：当是否收藏单选框绑定的值发生变化之后就会立即调用
    collectChange () {
      // 获取全部或者收藏的素材
      this.loadImages()
    },
    // 取消操作
    cancel () {
      // 关闭弹框
      this.dialogVisible = false
      // 图片和缓存区清空
      this.curImage = null
      // 选中状态的图片恢复
      this.imageList.forEach(image => {
        image.isFlag = false
      })
    },
    // 确定操作
    confirm () {
      // this.curImage 代表最终要提交给父组件的数据
      const confirmInageData = this.curImage
      // 正式将确定的数据提交到父组件中
      this.$emit('update:imgurl', confirmInageData.url)
      // 将弹框关闭
      this.dialogVisible = false
    }

  },
  mounted () {
    this.loadImages()
  }
}
</script>

<style lang="less" scoped>
.my-cover{
  display: inline-block;
}
.operateBox{
  display: inline-block;
  width: 100px;
  height: 100px;
  margin-left: 10px;
  border-radius: 4px;
  border:1px solid #ccc;
  font-size:13px;
  color:#666;
  text-align: center;
  line-height: 100px;
  overflow: hidden;
  cursor: pointer;
  position: relative;
  .img{
    position: absolute;
    top:0;
    left:0;
    width: 100%;
    height: 100%;
  }
}
  .img_item {
    position: relative;
    box-sizing: border-box;
    .img_box{
      height: 120px;
      margin-bottom: 10px;
       position: relative;
      img{
        width: 100%;
        height: 100%;
      }
      .selected{
        position: absolute;
        width: 20%;
        left:0;
        top:0;
      }
    }
  }
  .option {
    position: absolute;
    left: 5px;
    right:5px;
    bottom: 5px;
    height: 30px;
    background: rgba(0, 0, 0, 0.3);
    color: #fff;
    display: flex;
    span {
      flex:1;
      text-align: center;
      cursor: pointer;
      line-height: 30px;
    }
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .uploadBox{
    text-align: center;
    img {
      width:100%;
    }
    // scoped代表当前的样式只能影响当前的组件  影响不了子组件的样式
    // 加上 /deep/ 之后可以进行深度样式控制
    // 当试图直接控制子组件内部元素样式的时候就需要添加 /deep/
    /deep/.el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
  }
</style>>
